package com.crm.service.impl.situationAware;

import com.crm.common.util.KeyWordComputer;
import com.crm.common.util.Keyword;
import com.crm.dao.mapper.companySpider.CompanySpiderMapper;
import com.crm.dao.mapper.companySpider.FinancialNewsMapper;
import com.crm.dao.mapper.customMapper.spiderCustomMapper.SpiderCustomMapper;
import com.crm.model.entity.companySpider.CompanySpider;
import com.crm.model.entity.companySpider.CompanySpiderExample;
import com.crm.model.entity.companySpider.FinancialNews;
import com.crm.model.entity.companySpider.FinancialNewsExample;
import com.crm.model.vo.situationAware.FinancialNewsVO;
import com.crm.model.vo.situationAware.SpiderDataVO;
import com.crm.service.situationAware.SpiderDbService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yuanyunfeng
 * @date 2021/6/3 9:30
 */
@Service
@Slf4j
public class SpiderDbServiceImpl implements SpiderDbService {

    @Autowired
    private SpiderCustomMapper spiderCustomMapper;

    @Autowired
    private FinancialNewsMapper financialNewsMapper;

    @Override
    public List<SpiderDataVO> querySpiderData(String companyName) {
        List<SpiderDataVO> spiderList = spiderCustomMapper.querySpiderList(companyName);
        log.info("查询数据库返回结果【{}】", new Gson().toJson(spiderList));
        return spiderList;
    }

    @Override
    public void insertFinancialNews(FinancialNews financialNews) {
        FinancialNewsExample newsEx = new FinancialNewsExample();
        newsEx.createCriteria().andNewsDateEqualTo(financialNews.getNewsDate())
                .andNewsTimeEqualTo(financialNews.getNewsTime())
                .andNewsContentEqualTo(financialNews.getNewsContent());
        List<FinancialNews> list = financialNewsMapper.selectByExample(newsEx);
        if (list == null || list.size() <= 0) {
            financialNewsMapper.insertSelective(financialNews);
        }

    }

    @Override
    public List<FinancialNews> getFinancialNews(String newsDate) {
        FinancialNewsExample newsExample = new FinancialNewsExample();
        if(StringUtils.isNotBlank(newsDate)){
            newsExample.createCriteria().andNewsDateEqualTo(newsDate);
        }
        newsExample.setOrderByClause("news_date desc,news_time desc");
        List<FinancialNews> financialNews = financialNewsMapper.selectByExample(newsExample);
        return financialNews;
    }

    @Override
    public void updateKeyWords() {
        FinancialNewsExample financialEx = new FinancialNewsExample();
        financialEx.createCriteria().andNewsKeyIsNull();//只更新关键字为空的
        List<FinancialNews> financialNews = financialNewsMapper.selectByExample(financialEx);
        FinancialNews financal = new FinancialNews();
        for (FinancialNews finance : financialNews) {
            StringBuilder sb = new StringBuilder();
            String keyStr = ",";
            if (Objects.nonNull(finance.getNewsContent()) && StringUtils.isBlank(finance.getNewsKey())) {
                KeyWordComputer key = new KeyWordComputer(5);
                Iterator it = key.computeArticleTfidf(finance.getNewsContent()).iterator();
                while (it.hasNext()) {
                    Keyword key2 = (Keyword) it.next();
                    sb.append(key2 + keyStr);
                }
            }
            keyStr = sb.toString();
            log.info("打印这个字符串【{}】", keyStr);
            if (StringUtils.isNotBlank(keyStr)) {
                keyStr = keyStr.substring(0, keyStr.length() - 1);
                finance.setNewsKey(keyStr);
                financialNewsMapper.updateByPrimaryKeySelective(finance);
            }
        }
    }



}
